package com.midian.controller.version;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.midian.common.dao.OrderHelper;
import com.midian.common.filter.annotation.Validate;
import com.midian.common.pager.Pager;
import com.midian.common.system.RetManager;
import com.midian.common.utils.ContentJSONObject;
import com.midian.model.version.AndroidVersion;
import com.midian.service.version.IAndroidVersionService;

@Controller("versionController")
public class VersionController {
	/** 日志 */
	private static Logger LOG = Logger.getLogger(VersionController.class);

	/** 设备号表业务 */
	private IAndroidVersionService androidVersionService;

	@Resource(name = "androidVersionService")
	public void setAndroidVersionService(IAndroidVersionService androidVersionService) {
		this.androidVersionService = androidVersionService;
	}

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/android_version", method = RequestMethod.GET)
	public @ResponseBody
	Object organizationJoinApply(String client_key) throws Exception {
		JSONObject json = new JSONObject();
		try {
			List<OrderHelper> orderHelpers = new ArrayList<>();
			OrderHelper orderHelper = new OrderHelper();
			orderHelper.setPropertyName("createTime");
			orderHelper.setType(OrderHelper.Type.DESC);
			orderHelpers.add(orderHelper);
			List<AndroidVersion> androidVersions = androidVersionService.showList(new AndroidVersion(), new Pager(), orderHelpers);
			ContentJSONObject contentJson = new ContentJSONObject();
			if (androidVersions.isEmpty()) {
				contentJson.put("file_name", "");
				contentJson.put("version", "");
				contentJson.put("detail", "");
			} else {
				AndroidVersion androidVersion = androidVersions.get(0);
				contentJson.put("file_name", androidVersion.getFileName());
				contentJson.put("version", androidVersion.getVersion());
				contentJson.put("detail", androidVersion.getDetail());
			}
			json.put("content", contentJson.getContent());
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

}
